package br.com.ordersys.web.client.modules.main;

import java.util.Date;

import br.com.ordersys.web.client.modules.uicomp.FallbackImage;
import br.com.ordersys.web.client.modules.utils.I18nBundle;
import br.com.ordersys.web.client.modules.utils.UIUtils;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class UserOptionsPanel extends SimplePanel {
	PopupPanel popupOwner;
	I18nBundle i18n = new I18nBundle("mainWindow");
	
	static{
		MainWindowResources.INSTANCE.mainWindowStyle().ensureInjected();
	}

	public UserOptionsPanel(PopupPanel popupOwner) {
		super();
		this.popupOwner = popupOwner;
		buildUI();
	}

	private void buildUI() {
		setSize("100%", "100%");

		VerticalPanel vPanel = new VerticalPanel();
		vPanel.setSize("100%", "100%");
		setWidget(vPanel);

		HorizontalPanel hPnl = new HorizontalPanel();
		// UIUtils.setWidgetStyle(hPnl, "borderWidth: 0 0 1px; borderStyle: solid; width: 100%; height: 12em");

		vPanel.add(hPnl);

		vPanel.setCellVerticalAlignment(hPnl, vPanel.ALIGN_MIDDLE);

		final FallbackImage img = new FallbackImage("img/user/" + UIUtils.getEnvironment("userId") + "?t=" + new Date().getTime());
		img.setFallbackURL("images/silueta-homen-azul.png");
		img.setSize("10em", "10em");

		hPnl.add(img);
		hPnl.setCellHorizontalAlignment(img, hPnl.ALIGN_CENTER);
		hPnl.setCellVerticalAlignment(img, hPnl.ALIGN_MIDDLE);
		UIUtils.setParentStyle(img, "width: 10em; height: 10em");

		img.setVisible(false);
		Panel centerPanel = buildCenterPanel();

		hPnl.add(centerPanel);
		UIUtils.setParentStyle(centerPanel, "height: 100%");

		hPnl.setCellHorizontalAlignment(centerPanel, hPnl.ALIGN_LEFT);
		
	}

	private Panel buildCenterPanel() {
		VerticalPanel pnl = new VerticalPanel();
		UIUtils.setWidgetStyle(pnl, "paddingLeft: 6px");

		HTML lblName = new HTML(UIUtils.getEnvironment("userName"));
		lblName.setStyleName(MainWindowResources.INSTANCE.mainWindowStyle().boldText());

		pnl.add(lblName);

		HTML lblEmail = new HTML(UIUtils.getEnvironment("userEmail"));
		lblEmail.setStyleName(MainWindowResources.INSTANCE.mainWindowStyle().normalText());

		pnl.add(lblEmail);

		SimplePanel spnl = new SimplePanel(); // painel necessário apenas por conta do posicionamento absoluto
		UIUtils.setWidgetStyle(spnl, "position: absolute; bottom: 4px; width: 100%");

		HorizontalPanel hPnl = new HorizontalPanel();
		//UIUtils.setWidgetStyle(hPnl, "width: 100%");

		spnl.setWidget(hPnl);

		Hyperlink lnkAlteraFoto = new Hyperlink();
		lnkAlteraFoto.setText(i18n.get("lbl_modify_pict"));

		lnkAlteraFoto.addHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent e) {
				changePicture();
			}
		}, ClickEvent.getType());

		hPnl.add(lnkAlteraFoto);
		UIUtils.setParentStyle(lnkAlteraFoto, "paddingLeft: 1em");

		Hyperlink lnkAlterarSenha = new Hyperlink();
		lnkAlterarSenha.setText(i18n.get("lbl_modify_password"));
		lnkAlterarSenha.addHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent e) {
				changePassword();
			}
		}, ClickEvent.getType());

		hPnl.add(lnkAlterarSenha);
		UIUtils.setParentStyle(lnkAlterarSenha, "paddingRight: 2em; paddingLeft: 2em");

		pnl.add(spnl);
		pnl.setCellVerticalAlignment(spnl, pnl.ALIGN_BOTTOM);

		return pnl;
	}

	private void changePicture() {
		popupOwner.hide();
		PictureUploadForm picForm = new PictureUploadForm();

		picForm.show();

	}
	
	private void changePassword(){
		popupOwner.hide();
		GWT.runAsync(new RunAsyncCallback() {
			@Override
			public void onSuccess() {
				PasswordChangeForm passForm = new PasswordChangeForm();

				passForm.show();
			}

			@Override
			public void onFailure(Throwable e) {
				Window.alert("erro 'Code split': " + e.getMessage());
			}
		});
	}

}
